﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：UserLogin.aspx.cs
//
//
//文件功能描述：顾客登录....
//
//
//创建标识：2007.11.19 李正平
//创建描述：
//
//
//修改：2007.11.28 李正平
//修改描述：对一些不合规范的代码进行了重新编写，并在登录时使用了异步登录，还添加了在系统正在登录时，顾客可以取消登录
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;


public partial class UserLogin : System.Web.UI.Page
{
    //
    //调用WebService服务，异步调用登录结果
    //
    protected localhost.CustomerService WSCustomer = new localhost.CustomerService();


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 对数据进行加密
    /// </summary>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    protected byte[] GetPassword(string strPassword)
    {

        //
        //转化为二进制数组
        //
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] arSource = UE.GetBytes(strPassword);

        //
        //哈希加密
        //
        SHA1 SHharsh = new SHA1Managed();
        byte[] arHarshValue = SHharsh.ComputeHash(arSource);

        //
        //返回一个二进制数组
        //
        return arHarshValue;
    }


    /// <summary>
    /// 点击登录按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //
        //判断输入的顾客用户名和密码是否为空
        //
        if ((txtUserName.Text.Trim() != string.Empty) && (txtPwd.Text.Trim() != string.Empty))
        {
            //
            //对密码进行加密
            //
            byte[] password = GetPassword(txtPwd.Text.Trim());

            //
            //调用异步实现登录
            //
            WSCustomer.CustomerLoginCompleted += new localhost.CustomerLoginCompletedEventHandler(WSCustomer_CustomerLoginCompleted);

            //
            //传入顾客登录名和顾客登录密码
            //
            WSCustomer.CustomerLoginAsync(txtUserName.Text.Trim(), password);
        }

   }
    
    /// <summary>
    /// 返回顾客是否登录成功
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void WSCustomer_CustomerLoginCompleted(object sender, localhost.CustomerLoginCompletedEventArgs e)
    {

        //
        //用户未取消异步操作且执行异步时未产生异常
        //
        if (!e.Cancelled || e.Error == null)
        {
            //
            //声明一个登陆是否成功的变量
            //
            bool success = e.Result;
            if (success)
            {
                //
                //登录成功
                //
                this.lblMessage.Text = string.Empty;

                //
                //新建一个顾客实例
                //
                localhost.CustomerService customer = new localhost.CustomerService();

                //
                //获取顾客ID
                //
                string customerID = customer.SelectCustomerID(this.txtUserName.Text.Trim());

                //
                //保存顾客ID
                //
                Session["CustomerID"] = (string)customerID;

                //
                //登陆成功将未登录之前购买的商品存入到数据库中
                //
                if (Session["CartList"] != null)
                {
                    //
                    //获取顾客在未登录之前放入购物车内的商品
                    //
                    DataSet dsCartList = (DataSet)Session["CartList"];

                    //
                    //实例一个写购物车操作的类
                    //
                    localhost.CartService CartOperate = new localhost.CartService();

                    //循环写入数据库
                    for (int i = 0; i < dsCartList.Tables[0].Rows.Count; i++)
                    {
                        try
                        {
                            //
                            //往购物车中加入商品ID

                            //2007.12.5 翟丛兴修改修改为调用webServcie的cart的转移session购物车中的商品到数据库中的购物车
                            //CartOperate.Add(customerID, dsCartList.Tables[0].Rows[i]["ProductID"].ToString());

                            localhost.Cart cartObj = new localhost.Cart();
                            cartObj.CustomerID = customerID;
                            cartObj.ProductID = dsCartList.Tables[0].Rows[i]["ProductID"].ToString();
                            cartObj.Quantity = Convert.ToInt32(dsCartList.Tables[0].Rows[i]["Quantity"].ToString());
                            CartOperate.TransferToDB(cartObj);

                           
                        }
                        catch (Exception ex)
                        {
                            //
                            //抛出异常
                            //
                            throw ex;
                        }
                    }
                }

                //
                //成功登录之后，转到顾客初始访问页面
                //                
                if (FormsAuthentication.GetRedirectUrl(customerID, false).IndexOf("UserLogin.aspx") == -1)
                {
                    FormsAuthentication.RedirectFromLoginPage(customerID, false);
                }
                else
                {
                    Server.Transfer("MyInformation.aspx");
                }
            }
            else
            {
                //
                //登录失败
                //
                this.lblMessage.Text = "用户名或密码错误";

                //
                //将登录名框设为空
                //
                this.txtUserName.Text = string.Empty;

                //
                //将密码设为空
                //
                this.txtPwd.Text = string.Empty;

            }
        }
        else
        {
            //
            //顾客取消登录或者系统在执行异步的时抛出异常
            //
            lblMessage.Text = "系统错误，请稍等！";
            throw e.Error;
        }

    }


    /// <summary>
    /// 取消登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //
        //将文本框设置为空
        //
        txtUserName.Text = string.Empty;
        txtPwd.Text = string.Empty;

        //
        //顾客点击取消登录
        //
        WSCustomer.CancelAsync(null);
    }

}
